home *** CD-ROM | disk | FTP | other *** search
/ Internet Publisher's Toolbox 2.0 / Internet Publisher's Toolbox.iso / java / applets / pythag~1 / filltr~1.jav < prev    next >
Encoding:
Text File  |  1995-10-31  |  4.7 KB  |  149 lines

  1. /*----------------------------------------------------------------------*/
  2.  
  3. /*----------------------------------------------------------------------*/
  4. /* fillTriangle  - simply draws a solid triangle  
  5.                  - the hope was to make it quick with integer
  6.                    calculations (short would do but java need me to do 
  7.                    explicit type casting)...well it works 
  8.                  - I'm using the drawLine routine..since all the lines 
  9.                    are parallel to the axis this is not optimal..possibly
  10.                    using drawRect would be quicker
  11.                  - this routine will most likely be obsolete with newer 
  12.                    versions of java so I will leave it as is--ugly      */
  13. /*----------------------------------------------------------------------*/
  14. /*               Jim Morey  -  morey@math.ubc.ca  - Aug 5               */
  15. /*----------------------------------------------------------------------*/
  16.   
  17. import java.io.InputStream;
  18. import java.awt.*;
  19. import java.net.*;
  20.  
  21. /*----------------------------------------------------------------------*/
  22.  
  23. class fillTriangle{
  24.   int vect[][]; /* .. coordinates of the 3 vertices .. */
  25.   private Graphics gr;
  26.  
  27.   fillTriangle (Graphics g) {
  28.     gr = g;
  29.     vect = new int[3][2];
  30.   }
  31.     
  32.   public void run(){
  33.     int xmax=0,xmin=0,ymax=0,ymin=0,bad,good, v[][], best,i,j;
  34.     int top[],bottom[],t_inc,b_inc,t_rem,b_rem,t_len,b_len,t_hei,b_hei; 
  35.     int t_temp, b_temp,t_one,b_one;
  36.  
  37.     v = new int[3][2];
  38.  
  39.     /* .. decide which axis is good to draw with -- it will save 
  40.           on calculations especially with skinny triangles .. */
  41.  
  42.     for(i=0;i<3;i++){
  43.       if (vect[i][0]>vect[xmax][0]) xmax = i;
  44.       if (vect[i][0]<vect[xmin][0]) xmin = i;
  45.       if (vect[i][1]>vect[ymax][1]) ymax = i;
  46.       if (vect[i][1]<vect[ymin][1]) ymin = i;
  47.     }
  48.  
  49.     /* .. sort according to the 'good axis' */
  50.     if (vect[xmax][0]-vect[xmin][0] > vect[ymax][1]-vect[ymin][1]) {
  51.       good = 1;
  52.       bad = 0;
  53.       v[0][0] = vect[ymin][0];
  54.       v[0][1] = vect[ymin][1];
  55.       v[1][0] = vect[3-ymax-ymin][0];
  56.       v[1][1] = vect[3-ymax-ymin][1];
  57.       v[2][0] = vect[ymax][0];
  58.       v[2][1] = vect[ymax][1];
  59.     } else{
  60.       good = 0;
  61.       bad = 1;
  62.       v[0][0] = vect[xmin][0];
  63.       v[0][1] = vect[xmin][1];
  64.       v[1][0] = vect[3-xmax-xmin][0];
  65.       v[1][1] = vect[3-xmax-xmin][1];
  66.       v[2][0] = vect[xmax][0];
  67.       v[2][1] = vect[xmax][1];
  68.     } 
  69.  
  70.     /* .. top and bottom may not be the "top" and "bottom" but these are the 
  71.           points that will trace out the triangle with the top point going 
  72.           over the middle vertex.. */
  73.  
  74.     top = new int[2];
  75.     bottom = new int[2];
  76.  
  77.     /* .. draw the first part of the triangle up till the middle vertex .. */
  78.     bottom[bad] = v[0][bad];
  79.     b_len = v[2][good]-v[0][good];
  80.     b_hei = v[2][bad]-v[0][bad];
  81.     b_temp = b_len/2;
  82.     if (b_len!=0) {
  83.       b_one = (b_hei >0) ? 1 : -1;
  84.       b_inc = b_hei/b_len;
  85.       b_rem = b_hei - b_inc*b_len;
  86.       if (b_rem < 0 ) b_rem *= -1;
  87.  
  88.       top[bad] = v[0][bad];
  89.       t_len = v[1][good]-v[0][good];
  90.       t_hei = v[1][bad]-v[0][bad];
  91.       if (t_len!=0) {
  92.         t_one = (t_hei >0) ? 1 : -1;
  93.         t_inc = t_hei/t_len;
  94.         t_rem = t_hei - t_inc*t_len;
  95.         if (t_rem < 0 ) t_rem *= -1;
  96.         t_temp = t_len/2;
  97.     
  98.         for (i=v[0][good];i<=v[1][good];i++){
  99.           top[good] = i;
  100.           bottom[good] = i;
  101.           gr.drawLine(top[0],top[1], bottom[0], bottom[1]);
  102.           top[bad] += t_inc;
  103.           bottom[bad] += b_inc;
  104.           t_temp += t_rem;
  105.           b_temp += b_rem;
  106.           if (t_temp >= t_len) {
  107.             t_temp -= t_len;
  108.             top[bad]+=t_one;
  109.           }
  110.           if (b_temp >= b_len) {
  111.             b_temp -= b_len;
  112.             bottom[bad]+=b_one;
  113.           }
  114.         }
  115.       }
  116.  
  117.       /* .. draw the second part of the triangle .. */
  118.       top[bad] = v[1][bad];
  119.       t_len = v[2][good]-v[1][good];
  120.       t_hei = v[2][bad]-v[1][bad];
  121.       if (t_len!=0) {
  122.         t_one = (t_hei >0) ? 1 : -1;
  123.         t_inc = t_hei/t_len;
  124.         t_rem = t_hei - t_inc*t_len;
  125.         if (t_rem < 0 ) t_rem *= -1;
  126.         t_temp = t_len/2;
  127.     
  128.         for (i=v[1][good]+1;i<=v[2][good];i++){
  129.           top[good] = i;
  130.           bottom[good] = i;
  131.           gr.drawLine(top[0],top[1], bottom[0], bottom[1]);
  132.           top[bad] += t_inc;
  133.           bottom[bad] += b_inc;
  134.           t_temp += t_rem;
  135.           b_temp += b_rem;
  136.           if (t_temp >= t_len) {
  137.             t_temp -= t_len;
  138.             top[bad]+=t_one;
  139.           }
  140.           if (b_temp >= b_len) {
  141.             b_temp -= b_len;
  142.             bottom[bad]+=b_one;
  143.           }
  144.         }
  145.       }
  146.     }
  147.   }
  148. }
  149.